//
//  DLEmoticonView.m
//  IMTest
//
//  Created by mac on 2020/3/31.
//  Copyright © 2020 yanbo li. All rights reserved.
//

#import "DLEmoticonView.h"
#import "DLEmoticonItem.h"
#import "DLEmoticonLayout.h"
#import "DLEmoticonManager.h"
#import "UIColor+DGGUIKit.h"
#import "UIColor+Color.h"

@interface DLEmoticonView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray<DLEmoticonModel *> *emojiArray;

@property (nonatomic, strong) DLEmoticonLayout *emoticonLayout;
 
@end

@implementation DLEmoticonView {
    UICollectionView *_collectionView;
    CGFloat _colPadding, _rowPadding;
}

@synthesize maxColCount = _maxColCount;

- (instancetype)initWithEmoticonType:(DLEmoticonType)emoticonType maxColCount:(NSInteger)maxColCount maxRowCount:(NSInteger)maxRowCount {
    if (self = [super init]) {
        _maxColCount = maxColCount;
        _maxRowCount = maxRowCount;
        self.emoticonType = emoticonType;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor whiteColor] setDrakModeColor:[UIColor colorWithRGBA:35 g:35 b:35 a:1]];
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:self.emoticonLayout];
        [self addSubview:_collectionView];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[DLEmoticonItem class] forCellWithReuseIdentifier:NSStringFromClass([DLEmoticonItem class])];
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView setShowsHorizontalScrollIndicator:NO];
        [_collectionView setShowsVerticalScrollIndicator:NO];
        //添加分页视图
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        [self addSubview:pageControl];
        _pageControl = pageControl;
        _pageControl.userInteractionEnabled = NO;
        pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:90 / 255.0 green:184 / 255.0 blue:183 / 255.0 alpha:1];
        pageControl.pageIndicatorTintColor = [UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:229 / 255.0 alpha:1];
        pageControl.currentPage = 0;
        
        _collectionView.pagingEnabled = YES;
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _collectionView.frame = CGRectMake(0, 0, self.frame.size.width, 0.8 * self.frame.size.height);
    CGFloat width = _collectionView.frame.size.width - 2 * self.margin;
    CGFloat height = _collectionView.frame.size.height - 2 * self.margin;
    _colPadding = (width - self.itemSize.width * self.maxColCount) / (self.maxColCount - 1);
    _rowPadding = (height - self.itemSize.height * self.maxRowCount) / (self.maxRowCount - 1);
    self.emoticonLayout.minimumLineSpacing = _colPadding;
    self.emoticonLayout.minimumInteritemSpacing = _rowPadding;
    self.emoticonLayout.itemSize = self.itemSize;
    self.emoticonLayout.sectionInset = UIEdgeInsetsMake(self.margin, self.margin, self.margin, self.margin);
    self.pageControl.frame = CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.frame.size.width, 10);
    
}

#pragma mark - setter
- (void)setEmoticonType:(DLEmoticonType)emoticonType {
    _emoticonType = emoticonType;
    self.emojiArray = nil;
    switch (emoticonType) {
        case DLEmoticonType_GG:
            self.emojiArray = [DLEmoticonManager ggEmoticonList];
            break;
        case DLEmoticonType_Emoji:
            self.emojiArray = [DLEmoticonManager emojiEmoticonList];
            break;
    }
    [_collectionView reloadData];
}

- (void)setMaxColCount:(NSInteger)maxColCount {
    _maxColCount = maxColCount;
    self.emoticonLayout.maxColCount = maxColCount;
}

#pragma mark - getter
- (DLEmoticonLayout *)emoticonLayout {
    if (!_emoticonLayout) {
        _emoticonLayout = [[DLEmoticonLayout alloc] init];
        _emoticonLayout.sectionInset = UIEdgeInsetsMake(self.margin, self.margin, self.margin, self.margin);
        _emoticonLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _emoticonLayout;
}

- (NSInteger)maxColCount {
    if (_maxColCount == 0) {
        _maxColCount = 7;
    }
    return _maxColCount;
}

- (NSInteger)maxRowCount {
    if (_maxRowCount == 0) {
        _maxRowCount = 3;
    }
    return _maxRowCount;
}

- (CGSize)itemSize {
    if (CGSizeEqualToSize(CGSizeZero, _itemSize)) {
        _itemSize = CGSizeMake(30, 30);
    }
    return _itemSize;
}

- (CGFloat)margin {
    if (_margin == 0) {
        _margin = 10;
    }
    return _margin;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSInteger maxCountOfsection = (self.maxRowCount * self.maxColCount) - 1;
    NSInteger sectionCount = self.emojiArray.count / maxCountOfsection;
    if (self.emojiArray.count % maxCountOfsection > 0) {
        sectionCount++;
    }
    self.pageControl.numberOfPages = sectionCount;
    return sectionCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger maxCountOfsection = self.maxRowCount * self.maxColCount;
    return maxCountOfsection;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DLEmoticonItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DLEmoticonItem class]) forIndexPath:indexPath];
    if (indexPath.item == self.maxRowCount * self.maxColCount - 1) {
        item.imageView.image = [UIImage imageNamed:@"DGGUIKit.bundle/btn_clean"];
        return item;
    }
    NSInteger maxCountOfsection = self.maxRowCount * self.maxColCount - 1;
    NSInteger index = MAX(0, indexPath.item + indexPath.section * maxCountOfsection);
    if (index < self.emojiArray.count) {
        DLEmoticonModel *model = [self.emojiArray objectAtIndex:index];
        item.imageView.image = [UIImage imageNamed:model.bundleFile];
    } else {
        item.imageView.image = nil;
    }
    return item;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger maxCountOfsection = self.maxRowCount * self.maxColCount - 1;
    NSInteger index = MAX(0, indexPath.item + indexPath.section * maxCountOfsection);
    if (indexPath.item == maxCountOfsection && [self.delegate respondsToSelector:@selector(emoticonViewDidBackspace:)]) {
        [self.delegate emoticonViewDidBackspace:self];
    } else if (index < self.emojiArray.count && [self.delegate respondsToSelector:@selector(emoticonView:itemDidClick:)]) {
        DLEmoticonModel *model = [self.emojiArray objectAtIndex:index];
        [self.delegate emoticonView:self itemDidClick:model];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageControl.currentPage = index;
}

@end
